package com.ellricky.baselib.ui.base;

import android.view.View;
import android.widget.AdapterView;

import com.andr.common.http.callback.BaseCallback;
import com.andr.common.http.callback.JOCallback;
import com.andr.common.http.util.JsonUtil;
import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.widget.refresh.RefreshFlag;
import com.ellricky.baselib.entity.PersonItemEntity;
import com.ellricky.baselib.adapter.PersonAdapter;
import com.ellricky.baselib.entity.PersonEntity;
import com.ellricky.baselib.listener.PersonListListener;
import com.ellricky.baselib.ui.base.BaseRefreshActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public abstract class PersonListActivity extends BaseRefreshActivity<Void, BasePresenter<Void>, PersonEntity>
        implements PersonListListener {

    private boolean choosePage = false;
    private boolean showRateBar = false;
    private boolean showClassNum = false;
    private boolean showSex = false;

    protected boolean isChoosePage() {
        return false;
    }

    protected boolean isShowRateBar() {
        return true;
    }

    public boolean isShowClassNum() {
        return showClassNum;
    }

    public boolean isShowSex() {
        return showSex;
    }

    @Override
    public void initUIView() {
        showRateBar = isShowRateBar();
        choosePage = isChoosePage();
        showClassNum = isShowClassNum();
        showSex = isShowSex();
        super.initUIView();
    }

    @Override
    protected void loadDataOnInit() {
        rvView.init(this, RefreshFlag.REFRESH);
        super.loadDataOnInit();
    }

    @Override
    protected BaseCallback getResponseCallback() {
        return new JOCallback(this) {
            @Override
            public void onSuccess(JSONObject dataJO, String msg) {
                dataSource.clear();
                JSONObject realDataJO = dataJO.optJSONObject("data");
                if (realDataJO != null) {
                    String title = realDataJO.optString("title");
                    if (!EmptyUtil.isEmpty(title)) {
                        String[] tArr = title.split(",");

                        for (String t : tArr) {
                            PersonEntity personEntity = new PersonEntity();
                            personEntity.setShortName(t);

                            JSONArray dataA = realDataJO.optJSONArray(t);
                            if (dataA != null){
                                List<PersonItemEntity> itemList =
                                        JsonUtil.toBeanList(dataA.toString(),PersonItemEntity.class);
                                personEntity.setData(itemList);
                            }
                            dataSource.add(personEntity);
                        }

                    }
                }

                rvView.onLoadFinish(dataSource, refresh);
            }

            @Override
            public void onErrorCode(JSONObject dataJO, String code, String msg) {
                super.onErrorCode(dataJO, code, msg);
                rvView.onRefreshFailed(refresh);
            }

            @Override
            public void onHttpError(String code, String msg) {
                super.onHttpError(code, msg);
                rvView.onRefreshFailed(refresh);
            }
        };

    }

    @Override
    protected BaseRecyclerAdapter<PersonEntity> getRvAdapter() {
        PersonAdapter personAdapter = new PersonAdapter(this, null, this);
        personAdapter.setChoosePage(choosePage);
        personAdapter.setShowRateBar(showRateBar);
        personAdapter.setShowClassNum(showClassNum);
        personAdapter.setShowSex(showSex);
        adapter = personAdapter;
        return adapter;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        super.onItemClick(adapterView, view, position, l);
        // do none
    }

    @Override
    public void onClickPersonItem(PersonItemEntity personItemEntity) {

    }
}
